package com.example.d_app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class Regist extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText ed_password;
    private EditText ed_repassword;
    private EditText et_phone;
    private EditText ed_add;
    private Button button_regist;
    private Button button_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        ed_password = (EditText) findViewById(R.id.ed_password);
        ed_repassword = (EditText) findViewById(R.id.ed_repassword);
        et_phone = (EditText) findViewById(R.id.et_phone);
        ed_add = (EditText) findViewById(R.id.ed_add);
        button_regist = (Button) findViewById(R.id.button_regist);
        button_cancel = (Button) findViewById(R.id.button_cancel);

        button_regist.setOnClickListener(this);
        button_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_regist:
                submit();
                break;
            case R.id.button_cancel:
                et_name.setText("");
                ed_password.setText("");
                ed_repassword.setText("");
                et_phone.setText("");
                ed_add.setText("");
                break;
        }
    }

    private void submit() {
        // validate
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String password = ed_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String repassword = ed_repassword.getText().toString().trim();
        if (TextUtils.isEmpty(repassword)) {
            Toast.makeText(this, "repassword不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "phone不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String add = ed_add.getText().toString().trim();
        if (TextUtils.isEmpty(add)) {
            Toast.makeText(this, "add不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!password.equals(repassword)){
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        User user =new User(name,password,phone,add,false);
        user.saveUserBySharedPreferences(this);
        Intent intent=new Intent(this,Change.class);
        Bundle bundle=new Bundle();
        intent.putExtra("name",user.getName());
        intent.putExtra("pwd",user.getPwd());
        intent.putExtra("phone",user.getPhone());
        intent.putExtra("add",user.getAdd());
        startActivity(intent);
    }
}
